package com.chenfan.ccp.plug.mybatis.handler;

import com.chenfan.ccp.plug.mybatis.util.BaseSqlHelper;
import com.chenfan.ccp.plug.mybatis.mapper.MybatisBaseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;


/**
 * mybatis的注解拦截处理器
 */

public class MybatisAnnotationHandler {

	private Logger log= LoggerFactory.getLogger(getClass());

	private final String EMPTY = "";


	private boolean isEmpty(BaseSqlHelper baseSqlHelper) {
		if (baseSqlHelper == null || baseSqlHelper.getSql() == null || baseSqlHelper.getSql().isEmpty()) {
			log.warn("sql param was empty");
			return true;
		}
		return false;
	}

	public String update(BaseSqlHelper baseSqlHelper) {

		if (isEmpty(baseSqlHelper)) {
			return EMPTY;
		}

		return baseSqlHelper.getSql();

	}


	public String selectOne(BaseSqlHelper baseSqlHelper) {
		if (isEmpty(baseSqlHelper)) {
			return EMPTY;
		}

		return baseSqlHelper.getSql();

	}

	public String selectOneByCache(BaseSqlHelper baseSqlHelper) {
		if (isEmpty(baseSqlHelper)) {
			return EMPTY;
		}

		return baseSqlHelper.getSql();

	}


	public String selectAll(BaseSqlHelper baseSqlHelper) {
		if (isEmpty(baseSqlHelper)) {
			return EMPTY;
		}

		return baseSqlHelper.getSql();

	}
	public String selectAllByCache(BaseSqlHelper baseSqlHelper) {
		if (isEmpty(baseSqlHelper)) {
			return EMPTY;
		}

		return baseSqlHelper.getSql();

	}




	public String save(Map map) {
		if (map == null) {
			return EMPTY;
		}
		List<BaseSqlHelper> baseSqlHelpers = (List<BaseSqlHelper>) map.get(MybatisBaseMapper.LIST_PARAM);
		if (baseSqlHelpers.isEmpty()) {
			return EMPTY;
		}

		BaseSqlHelper baseSqlHelper = baseSqlHelpers.get(0);


		return baseSqlHelper.getSql();

	}

	public String delete(BaseSqlHelper baseSqlHelper) {
		if (isEmpty(baseSqlHelper)) {
			return EMPTY;
		}

		return baseSqlHelper.getSql();

	}


}
